package com.tjetc.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tjetc.common.FileUploadInfo;
import com.tjetc.common.JsonResult;
import com.tjetc.entity.User;
import com.tjetc.enums.EnumSex;
import com.tjetc.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("admin/users")
public class UserListController {
    @Autowired
    private UserService userService;

    @Autowired
    private FileUploadInfo fileUploadInfo;

    /**
     * 列出所有的User
     * @return 返回User列表封装的Json对象
     */
    @RequestMapping("list")
    public JsonResult<List<User>> selectAll(){
        return new JsonResult<>(0, "", userService.list());
    }

    /**
     * 分页查询User
     * @param pageNum 第几页
     * @param pageSize 每个多少个
     * @return 返回分页封装对象
     */
    @RequestMapping("page")
    public JsonResult<PageInfo> page(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                     @RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize){
        return new JsonResult<>(0, "", userService.page(pageNum, pageSize));
    }

    /**
     * 更新User
     * @param user 传入的user对象
     * @return 返回更新状态
     */
    @RequestMapping("update")
    public JsonResult update(User user){
        return new JsonResult(0, "", userService.update(user));
    }

    /**
     * 取消图片上传，将多余的图片删除
     * @param path 输入图片路径。注意不要把不应该删的删了
     * @return 返回状态
     */
    @RequestMapping("cancel")
    public JsonResult deleteUploadImage(@RequestParam("image") String path){
        return userService.deleteFile(fileUploadInfo.getImageBasePath() + path);
    }

    /**
     * 查询user表，分页查询
     * @param user user条件对象
     * @param pageNum 第几页
     * @param pageSize 每页几个
     * @return 返回分页的信息
     */
    @RequestMapping("search/fromUser")
    public JsonResult<PageInfo> pageFromUser(User user,
                                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                             @RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize) {
        return new JsonResult<>(0, "", userService.search2Page(user, pageNum, pageSize));
    }

    /**
     * 通过用户名查询
     * @param username 用户名
     * @param pageNum 第几页
     * @param pageSize 每页几个
     * @return 返回分页信息
     */
    @RequestMapping("search/fromUsername")
    public JsonResult<PageInfo> searchUsername2Page(@RequestParam("username") String username,
                                                   @RequestParam("pageNum") Integer pageNum,
                                                   @RequestParam("pageSize") Integer pageSize) {
        User user = new User();
        user.setUsername(username);
        return new JsonResult<>(0, "", userService.search2Page(user, pageNum, pageSize));
    }

    /**
     * 通过手机号查询
     * @param phone 手机号
     * @param pageNum 第几页
     * @param pageSize 每页几个
     * @return 返回分页信息
     */
    @RequestMapping("search/fromPhone")
    public JsonResult<PageInfo> searchPhone2Page(@RequestParam("phone") String phone,
                                                   @RequestParam("pageNum") Integer pageNum,
                                                   @RequestParam("pageSize") Integer pageSize) {
        User user = new User();
        user.setUsername(phone);
        return new JsonResult<>(0, "", userService.search2Page(user, pageNum, pageSize));
    }

    /**
     *
     * @param sex 性别
     * @param pageNum 第几个
     * @param pageSize 每页几个
     * @return 返回分页信息
     */
    @RequestMapping("search/fromSex")
    public JsonResult<PageInfo> searchSex2Page(@RequestParam("sex") Integer sex,
                                                 @RequestParam("pageNum") Integer pageNum,
                                                 @RequestParam("pageSize") Integer pageSize) {
        EnumSex enumSex = EnumSex.getByCode(sex);  // 转换为枚举类型
        User user = new User();
        user.setSex(enumSex);
        return new JsonResult<>(0, "", userService.search2Page(user, pageNum, pageSize));
    }

    /**
     * 批量删除
     * @param ids 想要批量删除的id列表
     * @return 返回删除状态
     */
    @RequestMapping("delete")
    public JsonResult deleteUsers(@RequestBody List<Integer> ids) {
        System.out.println(ids);
        Integer integer = userService.deleteUsers(ids);
        return new JsonResult<>(0, "", integer);
    }

    /**
     * 添加user
     * @param user 传入需要添加的user对象
     * @return 返回添加状态
     */
    @RequestMapping("add")
    public JsonResult addUser(User user) {
        return new JsonResult(0, "", userService.insert(user));
    }

    /**
     * 查询用户
     * @param id 用户id
     * @return 返回User对象
     */
    @RequestMapping("findUser")
    public JsonResult findUserById(@RequestParam("id") Integer id){
        return new JsonResult<>(0, "", userService.selectById(id));
    }
}
